package cn.weiron.fun.infrastructure.dto;

import cn.weiron.fun.infrastructure.auth.Authority;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;

/**
 * <p>用户登录后用户的详细信息</p>
 */
@JsonIgnoreProperties(ignoreUnknown = true)
public class LoggedAccountDto implements Authority.Account {
    /**
     * 用户类型 0机构用户 1审核用户
     */
    private Integer type;

    /**
     * 用户ID
     */
    private String userId;

    /**
     * 用户名称
     */
    private String userName;

    /**
     * 角色名称
     */
    private String roleName;

    /**
     * 角色ID
     */
    private String roleId;

    /**
     * 机构编码
     */
    private String orgCode;

    /**
     * token
     */
    private String token;

    /**
     * 是否首次登录
     */
    private Boolean isFirstLogin;

    /**
     * 是否具有删除权限
     */
    private Boolean hasDelete;

    /**
     * 是否显示全部机构权限
     */
    private Boolean isShowAllAuditOrg=false;

    /**
     * 行政区域代码
     */
    private String areaCode;

    /**
     * 登录用户档案标识
     */
    private String personnelId;

    /**
     * 用户所属机构类型
     */
    private String orgType;

    /**
     * 检查账户配置是否通过鉴权，通过鉴权即可以使用对应的接口
     *
     * @param accountAuthKey 账户鉴权标识
     */
    @Override
    public boolean ok(String accountAuthKey) {
        return true;
    }

    /**
     * 自定义Token实现方法，默认使用UUID。
     */
    @Override
    public String customToken() {
        token = token == null || token.isEmpty() ? Authority.Account.super.customToken() : getToken();
        return token;
    }

    public Integer getType() {
        return type;
    }

    public void setType(Integer type) {
        this.type = type;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getRoleName() {
        return roleName;
    }

    public void setRoleName(String roleName) {
        this.roleName = roleName;
    }

    public String getRoleId() {
        return roleId;
    }

    public void setRoleId(String roleId) {
        this.roleId = roleId;
    }

    public String getOrgCode() {
        return orgCode;
    }

    public void setOrgCode(String orgCode) {
        this.orgCode = orgCode;
    }

    public String getToken() {
        return token;
    }

    public Boolean getFirstLogin() {
        return isFirstLogin;
    }

    public void setFirstLogin(Boolean firstLogin) {
        isFirstLogin = firstLogin;
    }

    public Boolean getHasDelete() {
        return hasDelete;
    }

    public void setHasDelete(Boolean hasDelete) {
        this.hasDelete = hasDelete;
    }

    public Boolean getIsShowAllAuditOrg() {
        return isShowAllAuditOrg;
    }

    public void setIsShowAllAuditOrg(Boolean showAllAuditOrg) {
        isShowAllAuditOrg = showAllAuditOrg;
    }

    public String getAreaCode() {
        return areaCode;
    }

    public void setAreaCode(String areaCode) {
        this.areaCode = areaCode;
    }

    public String getPersonnelId() {
        return personnelId;
    }

    public void setPersonnelId(String personnelId) {
        this.personnelId = personnelId;
    }

    public String getOrgType() {
        return orgType;
    }

    public void setOrgType(String orgType) {
        this.orgType = orgType;
    }

    @Override
    public void setToken(String token) {
        this.token = token;
    }

    /**
     * 转换器
     */
    public static class Assembler {
        /**
         * Json序列化
         */
        private static final ObjectMapper objectMapper = new ObjectMapper();

        /**
         * 序列化
         */
        public static String serializeValue(LoggedAccountDto loginPo) throws JsonProcessingException {
            return objectMapper.writeValueAsString(loginPo);
        }

        /**
         * 反序列化
         */
        public static LoggedAccountDto deserializeValue(String json) throws IOException {
            return objectMapper.readValue(json, LoggedAccountDto.class);
        }
    }
}
